{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=1793
  Verdict: Accepted
}
uses
	SysUtils, Math; 
 
const
	INF:int64 = round(1e9);
 
var
	n,k,b,m,i,j,cur,ans,l,r,c:longint;
	a,d,p,no:array[0..100010] of longint;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n,a[1],k,b,m);
for i:=2 to n do
	a[i]:=(k*a[i-1]+b) mod m;
for i:=2 to 100010 do
	d[i]:=INF;
d[1]:=-INF;
no[1]:=-1;
for i:=1 to n do begin
	l:=1;
	r:=n + 1;
	while r-l>1 do begin
		c:=(l + r) shr 1;
		if d[c] < a[i] then l:=c else r:=c;	
	end;
	j:=l;
	if (d[j] < a[i]) and (a[i] < d[j+1]) then begin
		d[j+1] := a[i];
		p[i] := no[j];
		no[j+1] := i;
	end;
end;
ans:=0;
for i:=n downto 2 do
	if d[i] <> INF then begin
		cur := no[i];
		while cur <> -1 do begin
			inc(ans);
			cur := p[cur];
		end;
		break;
	end;
write(ans);
end.